import { baseApiUrl, path } from "../../utils/config";
import { addressInfo, addressPost } from '../../utils/api';
import Toast from '@vant/weapp/toast/toast';
const app = getApp();
Page({
  data: {
    baseImageUrl: baseApiUrl,
    placeholderStyle: "font-size: 28rpx;color: #CACACA; font-weight:200;",

    id: null,
    name: "",
    phone: "",

    province: "",
    city: "",
    area: "",

    province_id: "",
    city_id: "",
    area_id: "",

    region: "",
    value: [],
    address: "",

    phone_list: ["+86"],
    show: false,
    is_default: false
  },
  async onLoad(options) {
    let id = options.id;
    if (!id) return false;
    let { address } = await addressInfo({ id });
    let region = `${address.province}${address.city}${address.area}`;
    let value = [address.province, address.city, address.area];
    address.region = region;
    address.value = value;
    this.setData(address);
  },
  showOpen() {
    this.setData({ show: !this.data.show });
  },
  changeChecked() {
    this.setData({ is_default: !this.data.is_default });
  },
  bindRegionChange(e) {
    let { value, code } = e.detail;
    let [province, city, area] = value;
    let [province_id, city_id, area_id] = code;
    let region = `${province}${city}${area}`;
    this.setData({ province, city, area, province_id, city_id, area_id, region });
  },
  async submit() {
    let data = {
      name: this.data.name,
      phone: this.data.phone,
      province: this.data.province,
      city: this.data.city,
      area: this.data.area,
      address: this.data.address,
      is_default: this.data.is_default
    };
    let id = this.data.id;
    id ? data['id'] = id : "";
    await addressPost(data);
    Toast.success('保存成功');
  },
  onShareAppMessage() {
    let poster = app.globalData.poster;
    return {
      path,
      imageUrl: poster
    }
  },
  onShareTimeline() { }
})